package com.woniuxy.rabbitmq.receiver;

import com.rabbitmq.client.Channel;
import com.woniuxy.service.AuctionService;
import com.woniuxy.util.JsonUtil;
import com.work.framework.pojo.BiddingRecord;
import com.work.framework.pojo.ResultDTO;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: wanghao
 * @Date: 2019/9/11 15:22
 */
@Component
public class AuctionReceiver {
    @Resource
    private AuctionService auctionService;

    @RabbitListener(queues = {"auctionQueue"}, containerFactory = "rabbitListenerContainerFactory")
    public void handleMessage(Message message, Channel channel) throws Exception {
        // 处理消息
        System.out.println("FirstConsumer {} handleMessage :" + new String(message.getBody()));
        BiddingRecord biddingRecord = JsonUtil.StringToObject(new String(message.getBody()));
        ResultDTO resultDTO = auctionService.addBiddingRecord(biddingRecord);
        if (resultDTO.getCode() == 200) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } else {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }

}
